//
//  ByOrderVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByOrderVC.h"
#import "ByCoreViewSegmentControl.h"
#import "ByOrderShowVC.h"

@interface ByOrderVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) ByCoreViewSegmentControl *segTitle;
@property (strong, nonatomic)  UIScrollView *svContent;

@property (nonatomic, strong) NSArray *titlesAry;
@end

@implementation ByOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    [self addLeftBackBarButtonItem];
    [self initViews];
    [self _initData];
}

#pragma mark ---getter
-(NSArray *)titlesAry{
    if (_titlesAry == nil) {
        _titlesAry = @[@"待付款",@"待发货",@"待收货",@"已完成"];
    }
    return _titlesAry;
}

#pragma makr --创建视图
- (void)initViews{
    self.segTitle = [[ByCoreViewSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
    [self.view addSubview:self.segTitle];
    
    self.svContent = [[UIScrollView alloc] init];
    [self.view addSubview:self.svContent];
    [self.svContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.segTitle.mas_bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        
    }];
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    self.segTitle.sectionTitles = [self.titlesAry copy];
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = [UIFont boldSystemFontOfSize:16.0];
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = HTB_TEXT_COLOR2;
    self.segTitle.selectedTextColor = HTB_THEME_COLOR;
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segTitle.selectionIndicatorHeight = 5;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        [weakSelf changeSelect:index animated:YES];
    }];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 1)];
    lbl.backgroundColor = [UIColor colorWithHexString:@"dbdbdb"];
    [self.segTitle addSubview:lbl];
    [self.view addSubview:self.segTitle];
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH*self.titlesAry.count, 200);
    self.svContent.delegate = self;
    [self performSelector:@selector(delaySelect) withObject:nil afterDelay:0.1];
    
}
- (void)delaySelect{
    [self changeSelect:0 animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segTitle setSelectedSegmentIndex:page animated:YES];
    [self addViewController:page];
}

- (void)changeSelect:(NSInteger)intSelect animated:(BOOL)animated{
    [self.segTitle setSelectedSegmentIndex:intSelect animated:animated];
    [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH*intSelect, 0) animated:animated];
    [self addViewController:intSelect];
}

- (void)addViewController:(NSInteger)index{
    NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[ByOrderShowVC class]]) {
            //每次进入都重新下载View
            //[maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
        }
    }
    
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]) {
        ByOrderShowVC *vc =  [[ByOrderShowVC  alloc] init];
        vc.order_state = index;
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(SCREEN_WIDTH*index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
        [self.svContent addSubview:vc.view];
        vc.view.tag = index;
    }
}



@end
